package com.dc.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.annotation.DataScope;
import com.dc.common.constant.CustomConstants;
import com.dc.common.core.LoginUser;
import com.dc.common.exception.ServiceException;
import com.dc.common.utils.ShiroUtil;
import com.dc.common.vo.ResultVo;
import com.dc.project.system.dao.SysMenuDao;
import com.dc.project.system.dto.SysMenuDto;
import com.dc.project.system.entity.SysMenu;
import com.dc.project.system.entity.SysRoleMenu;
import com.dc.project.system.service.SysMenuService;
import com.dc.project.system.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 系统菜单权限表(SysMenu)表服务实现类
 *
 * @author zhuangcy
 * @since 2021-06-04 12:11:55
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @Override
    public ResultVo queryById(Serializable id) {
        return ResultVo.success().data(this.getById(id));
    }

    /**
     * 查询系统菜单权限表
     *
     * @param dto
     * @return
     */
    @DataScope
    @Override
    public ResultVo queryByList(SysMenuDto dto) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysMenu::getMenuSlot, SysMenu::getMenuId);
        List<SysMenu> list = this.list(queryWrapper);
        return ResultVo.success().data(list);
    }

    /**
     * 新增系统菜单权限表
     *
     * @param sysMenu
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo add(SysMenu sysMenu) {
        if (null == sysMenu.getParentId()) {
            sysMenu.setParentId(0);
        }
        return ResultVo.verify(this.save(sysMenu));
    }

    /**
     * 编辑系统菜单权限表
     *
     * @param sysMenu
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo edit(SysMenu sysMenu) {
        if (null == sysMenu.getParentId()) {
            sysMenu.setParentId(0);
        }
        return ResultVo.verify(this.updateById(sysMenu));
    }

    /**
     * 根据主键删除系统菜单权限表
     *
     * @param id 主键
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo delete(Serializable id) {
        // 删除时顺带把与角色关联的id也删除
        // 这里是真删
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getMenuId, id);
        sysRoleMenuService.remove(queryWrapper);
        if (this.removeById(id)) {
            return ResultVo.success();
        }
        throw new ServiceException();
    }

    /**
     * 通过角色id查询菜单数据
     *
     * @param roleId
     * @return
     */
    @Override
    public ResultVo getRoleMenu(Integer roleId) {
        LoginUser userInfo = ShiroUtil.getUserInfo();
        List<SysMenu> menus = null;
        if (Arrays.asList(CustomConstants.SUPER_ADMIN).contains(userInfo.getAccount())) {
            menus = this.list();
        } else {
            menus = baseMapper.getMenuByUser(userInfo.getUserId());
        }
        List<Integer> menuIds = sysRoleMenuService.getMenuIdByRoleId(roleId);
        HashMap<String, Object> result = new HashMap<>(2);
        result.put("menus", menus);
        result.put("menuIds", menuIds);
        return ResultVo.success().data(result);
    }
}